import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisDemoHash {

    public static void test1(Jedis jedis) {
        System.out.println("hget 和 hset");
        jedis.flushAll();

        jedis.hset("key", "field1", "value1");
        Map<String, String> fields = new HashMap<>();
        fields.put("field2", "value2");
        fields.put("field3", "value3");
        jedis.hset("key", fields);

        String value = jedis.hget("key", "field1");
        System.out.println("value: " + value);

        Map<String, String> values = jedis.hgetAll("key");
        System.out.println(values);
    }

    public static void test2(Jedis jedis) {
        System.out.println("hexists");
        jedis.flushAll();

        jedis.hset("key", "field1", "value1");
        jedis.hset("key", "field2", "value2");

        boolean exists = jedis.hexists("key", "field1");
        System.out.println("exists: " + exists);

        exists = jedis.hexists("key", "field2");
        System.out.println("exists: " + exists);
    }

    public static void test3(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "field1", "value1");
        jedis.hset("key", "field2", "value2");
        jedis.hset("key", "field3", "value3");

        long result = jedis.hdel("key", "field1");
        System.out.println("result: " + result);

        long result2 = jedis.hdel("key", "field2", "field3");
        System.out.println("result2: " + result2);
    }

    public static void test4(Jedis jedis) {
        System.out.println("hkeys 和 hvals");
        jedis.flushAll();

        jedis.hset("key", "field1", "value1");
        jedis.hset("key", "field2", "value2");
        jedis.hset("key", "field3", "value3");

        Set<String> fields = jedis.hkeys("key");
        List<String> vals = jedis.hvals("key");
        System.out.println("fields" + fields);
        System.out.println("vals" + vals);
    }

    public static void test5(Jedis jedis) {
        System.out.println("hmset 和 hmget");
        jedis.flushAll();

        Map<String, String> fields = new HashMap<>();
        fields.put("field1", "value1");
        fields.put("field2", "value2");
        fields.put("field3", "value3");
        jedis.hmset("key", fields);

        List<String> values = jedis.hmget("key", "field1", "field2", "field3");
        System.out.println("values: " + values);
    }

    public static void test6(Jedis jedis) {
        System.out.println("hdel");
        jedis.flushAll();

        jedis.hset("key", "field1", "value1");
        jedis.hset("key", "field2", "value2");
        jedis.hset("key", "field3", "value3");

        long result = jedis.hdel("key", "field1", "field2");
        System.out.println("result: " + result);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6666");

        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
            test6(jedis);
        }
    }
}
